package com.yc.swing.ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.ly.demo.taach.jdbc.impl.DeptDao;
import com.yc.swing.dao.DeptDaoSub;

import java.awt.FlowLayout;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ListSelectionModel;

public class DeptWin {

	private JFrame frame;
	private JTextField dnameTf;
	private JTextField locTf;
	private JTable table;

	// 部门管理数据库访问对象
	private DeptDao dao = new DeptDao();

	// 表头
	private Vector<String> columnNames = new Vector<>();
	// 表格数据
	private Vector<Vector<Object>> rowData = new Vector<>();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DeptWin window = new DeptWin();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public DeptWin() {
		initialize();
	}

	/**
	 * 	初始化方法, 构建界面自动生成代码
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setTitle("部门管理窗口");
		frame.setBounds(100, 100, 763, 515);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPanel ctrlPanel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) ctrlPanel.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		ctrlPanel.setPreferredSize(new Dimension(10, 30));
		frame.getContentPane().add(ctrlPanel, BorderLayout.NORTH);

		JLabel lblNewLabel = new JLabel("部门名称");
		ctrlPanel.add(lblNewLabel);

		dnameTf = new JTextField();
		ctrlPanel.add(dnameTf);
		dnameTf.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("部门位置");
		ctrlPanel.add(lblNewLabel_1);

		locTf = new JTextField();
		ctrlPanel.add(locTf);
		locTf.setColumns(10);

		JButton btnNewButton = new JButton("查询");
		// 自动生成点击事件代码
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				query();
			}
		});
		ctrlPanel.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("新增");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DeptEdit dialog = new DeptEdit();
				dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				dialog.setVisible(true);
				// 重新查询表格数据
				DeptWin.this.query();
			}
		});
		ctrlPanel.add(btnNewButton_1);

		JButton btnNewButton_2 = new JButton("修改");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 判断用户是否选择
				int selectedRowIndex = table.getSelectedRow();
				if (selectedRowIndex == -1) {
					JOptionPane.showMessageDialog(null, "请选择要修改的部门!");
					return;
				}

				// 获取选中的记录
				Vector<Object> row = rowData.get(selectedRowIndex);
				// 将数据传递给打开的 DeptEdit 窗口
				DeptEdit dialog = new DeptEdit(row);
				dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				dialog.setVisible(true);
				// 重新查询表格数据
				DeptWin.this.query();
			}
		});
		ctrlPanel.add(btnNewButton_2);

		JButton btnNewButton_3 = new JButton("删除");
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 判断用户是否选择
				int selectedRowIndex = table.getSelectedRow();
				if (selectedRowIndex == -1) {
					JOptionPane.showMessageDialog(null, "请选择要删除的部门!");
					return;
				}
				// 获取选中的记录
				Vector<Object> row = rowData.get(selectedRowIndex);

				// 提问用户是否删除    Scanner.next();
				int ret = JOptionPane.showConfirmDialog(null, "请确认是否要删除该部门", "系统提示", JOptionPane.YES_NO_OPTION);
				if (ret == JOptionPane.YES_NO_OPTION) {
					DeptDao dao = new DeptDaoSub();
					int deptno = Integer.parseInt("" + row.get(0));
					try {
						dao.deleteByDeptno(deptno);
						JOptionPane.showMessageDialog(null, "删除部门成功!");
					} catch (SQLException e1) {
						e1.printStackTrace();

						if (e1.getErrorCode() == -99) {
							// 自定义的SQL异常
							JOptionPane.showMessageDialog(null, e1.getMessage());
						} else {
							JOptionPane.showMessageDialog(null, "删除部门失败!");
						}
					}
				}

			}
		});
		ctrlPanel.add(btnNewButton_3);

		JScrollPane scrollPane = new JScrollPane();
		frame.getContentPane().add(scrollPane, BorderLayout.CENTER);

		columnNames.add("部门编号");
		columnNames.add("部门名称");
		columnNames.add("部门地址");
		table = new JTable(rowData, columnNames);
		scrollPane.setViewportView(table);

		query();

		frame.setLocationRelativeTo(null); // 窗口基于屏幕居中

		// 设置单选模式
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	}

	public void query() {
		try {
			// 清空原有表格数据
			rowData.clear();
			// 查询部门数据
			String dname = dnameTf.getText(); // 获取文本框的输入的字符串
			String loc = locTf.getText(); // 获取文本框的输入的字符串
			List<Map<String, Object>> list = dao.select(dname, loc);
			for (int i = 0; i < list.size(); i++) {
				Vector<Object> row = new Vector<>();
				row.add(list.get(i).get("deptno".toUpperCase()));
				row.add(list.get(i).get("dname".toUpperCase()));
				row.add(list.get(i).get("loc".toUpperCase()));
				rowData.add(row);
			}
			// 重新渲染界面数据
			table.updateUI();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

}
